package com.example.dragon.xmf;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dragon.adpter.PengDingAdpter;
import com.example.dragon.bean.OrderBean;
import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.example.dragon.dialog.WaitDialog;
import com.example.dragon.untis.CircleImageView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class CommentActivity extends AppCompatActivity {

    private CircleImageView mavter;
    private TextView mname;
    private MessageDialog messageDialog;
    private WaitDialog mwaitdiaglog;
    private String oid;
    private RatingBar ratingBar;
    private int stars=1;
    private EditText contentEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        initControls();
    }
    private void initControls(){
        Intent intent = getIntent();
        mwaitdiaglog = new WaitDialog (this,"2");
        mavter = (CircleImageView)findViewById(R.id.user_avatar);
        ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        contentEdit = (EditText)findViewById(R.id.content_edit);
        stars = ratingBar.getNumStars();
        oid = intent.getStringExtra("orderId");
        mname = (TextView)findViewById(R.id.dnameText);
        Glide.with(this).load(intent.getStringExtra("avter")).error(R.drawable.user_icon).into(mavter);
        mname.setText(intent.getStringExtra("dname"));
    }

    public void subComment(View view){
        Log.i("ss","ee");
        mwaitdiaglog.show();
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Orders&a=postComments")
                .addParams("token", MainActivity.TOKEN)
                .addParams("orders_id",oid)
                .addParams("content",contentEdit.getText().toString())
                .addParams("stars",stars+"")
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                        mwaitdiaglog.dismiss();
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200) {
                                mwaitdiaglog.dismiss();
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",CommentActivity.this);
                            }else {
                                mwaitdiaglog.dismiss();
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",CommentActivity.this);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


}
